package com.lqg.bookLibrary.adminController;

import com.alibaba.fastjson2.JSONObject;
import com.lqg.bookLibrary.common.core.domain.JsonResult;
import com.lqg.bookLibrary.pojo.MyList;
import com.lqg.bookLibrary.pojo.Roles;
import com.lqg.bookLibrary.pojo.Top;
import com.lqg.bookLibrary.pojo.Type;
import com.lqg.bookLibrary.service.IRolesService;
import com.lqg.bookLibrary.service.ITypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/manage/top")
@PreAuthorize("hasAnyRole('a')")
public class TopController {

    @Autowired
    private ITypeService typeService;

    @Autowired
    private IRolesService rolesService;

    @GetMapping("/user")
    public JsonResult user() {
        List<Top> list = new ArrayList<>();
        List<MyList> sexList = new ArrayList<>();
        sexList.add(new MyList(1, "男"));
        sexList.add(new MyList(0, "女"));

        List<MyList> roleList = new ArrayList<>();
        List<Roles> list1 = rolesService.list();

        roleList = list1.stream().map(x -> new MyList(x.getRoleUsName(), x.getRoleZhName()))
                .collect(Collectors.toList());

        list.add(new Top("userId", "id", false, "default", null));
        list.add(new Top("username", "用户名", true, "default", null));
        list.add(new Top("password", "密码", true, "password", null));
        list.add(new Top("sex", "性别", true, "list", sexList));
        list.add(new Top("email", "邮箱", true, "default", null));
        list.add(new Top("photo", "头像", true, "images", null));
        list.add(new Top("phone", "手机号", true, "default", null));
        list.add(new Top("role", "角色", true, "list", roleList));
        list.add(new Top("createdAt", "创建时间", false, "default", null));
        list.add(new Top("updatedAt", "更新时间", false, "default", null));

        return JsonResult.success(list);
    }

    @GetMapping("/book")
    public JsonResult book() {
        List<Top> list = new ArrayList<>();
        List<Type> typeList = typeService.lambdaQuery().isNotNull(Type::getFatherId).list();
        List<MyList> collect = typeList.stream().map(x -> new MyList(x.getTypeId(), x.getTypeName()))
                .collect(Collectors.toList());
        list.add(new Top("bookId", "id", false, "default", null));
        list.add(new Top("title", "书名", true, "default", null));
        list.add(new Top("author", "作者", true, "default", null));
        list.add(new Top("publisher", "出版商", true, "default", null));
        list.add(new Top("publicationDate", "发布日期", true, "default", null));
        list.add(new Top("summary", "简介", true, "default", null));
        list.add(new Top("imageUrl", "图片", true, "images", null));
        list.add(new Top("typeId", "类型", true, "list", collect));
        list.add(new Top("createdAt", "创建时间", false, "default", null));
        list.add(new Top("updatedAt", "更新时间", false, "default", null));

        return JsonResult.success(list);
    }

    @GetMapping("/type")
    public JsonResult type() {
        List<Top> list = new ArrayList<>();
        list.add(new Top("typeId", "id", false, "default", null));
        list.add(new Top("typeName", "名称", true, "default", null));
        list.add(new Top("fatherId", "父类", true, "default", null));

        return JsonResult.success(list);
    }

    @GetMapping("/borrow")
    public JsonResult borrow() {
        List<Top> list = new ArrayList<>();
        List<MyList> enableList = new ArrayList<>();
        enableList.add(new MyList(1, "是"));
        enableList.add(new MyList(0, "否"));
        list.add(new Top("borrowId", "借阅id", false, "default", null));
        list.add(new Top("userId", "用户id", false, "default", null));
        list.add(new Top("bookId", "图书id", false, "default", null));
        list.add(new Top("timeLimit", "借阅时长", true, "number", null));
        list.add(new Top("delay", "是否延期(1/0)", true, "list", enableList));
        list.add(new Top("overdue", "是否逾期(1/0)", true, "list", enableList));
        list.add(new Top("returnAt", "归还时间", false, "default", null));
        list.add(new Top("createdAt", "创建时间", false, "default", null));
        list.add(new Top("updatedAt", "更新时间", false, "default", null));

        return JsonResult.success(list);
    }

    @GetMapping("/role")
    public JsonResult role() {
        List<Top> list = new ArrayList<>();
        list.add(new Top("rolesId", "id", false, "default", null));
        list.add(new Top("roleUsName", "中文", true, "default", null));
        list.add(new Top("roleZhName", "英文", true, "default", null));

        return JsonResult.success(list);
    }

    @GetMapping("/menu")
    public JsonResult menu() {
        List<Top> list = new ArrayList<>();
        List<MyList> enableList = new ArrayList<>();
        enableList.add(new MyList(1, "是"));
        enableList.add(new MyList(0, "否"));
        list.add(new Top("menuId", "ID", false, "default", null));
        list.add(new Top("menuName", "菜单名称", true, "default", null));
        list.add(new Top("path", "菜单访问路径", true, "default", null));
        list.add(new Top("fatherId", "父菜单id", true, "default", null));
        list.add(new Top("enable", "是否启动菜单(1/0)", true, "list", enableList));
        list.add(new Top("query", "访问值", true, "default", null));
        return JsonResult.success(list);
    }

}
